<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划详情id" prop="planDetailsId">
        <el-input v-model="queryParams.planDetailsId" placeholder="请输入计划详情id" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="标段id" prop="bdId">
        <el-input v-model="queryParams.bdId" placeholder="请输入标段id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商id" prop="gysId">
        <el-input v-model="queryParams.gysId" placeholder="请输入供应商id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="材料编号" prop="manifestCode">
        <el-input v-model="queryParams.manifestCode" placeholder="请输入材料编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="材料名称" prop="manifestName">
        <el-input v-model="queryParams.manifestName" placeholder="请输入材料名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input v-model="queryParams.model" placeholder="请输入型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input v-model="queryParams.specification" placeholder="请输入规格" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="queryParams.unit" placeholder="请输入单位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="本月计划采购数量" prop="monthPlanPurchaseNumber">
        <el-input v-model="queryParams.monthPlanPurchaseNumber" placeholder="请输入本月计划采购数量" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="本次供应数量" prop="onceSupplyNumber">
        <el-input v-model="queryParams.onceSupplyNumber" placeholder="请输入本次供应数量" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="本月累计供应数量" prop="monthTotalSupplyNumber">
        <el-input v-model="queryParams.monthTotalSupplyNumber" placeholder="请输入本月累计供应数量" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="交接验收单据号" prop="acceptanceCode">
        <el-input v-model="queryParams.acceptanceCode" placeholder="请输入交接验收单据号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供货日期" prop="supplyDate">
        <el-date-picker clearable v-model="queryParams.supplyDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择供货日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8 cd-pr8 top-right-btn small-line-height">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ma:materialProgressDetails:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ma:materialProgressDetails:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ma:materialProgressDetails:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ma:materialProgressDetails:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border stripe v-loading="loading" :data="materialProgressDetailsList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="进度详情id" align="center" prop="progressDetailsId" v-if="true" />
      <el-table-column label="计划详情id" align="center" prop="planDetailsId" />
      <el-table-column label="标段id" align="center" prop="bdId" />
      <el-table-column label="供应商id" align="center" prop="gysId" />
      <el-table-column label="类型id" align="center" prop="type" />
      <el-table-column label="材料编号" align="center" prop="manifestCode" />
      <el-table-column label="材料名称" align="center" prop="manifestName" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="本月计划采购数量" align="center" prop="monthPlanPurchaseNumber" />
      <el-table-column label="本次供应数量" align="center" prop="onceSupplyNumber" />
      <el-table-column label="本月累计供应数量" align="center" prop="monthTotalSupplyNumber" />
      <el-table-column label="交接验收单据号" align="center" prop="acceptanceCode" />
      <el-table-column label="供货日期" align="center" prop="supplyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.supplyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ma:materialProgressDetails:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ma:materialProgressDetails:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改材料进度详情对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计划详情id" prop="planDetailsId">
          <el-input v-model="form.planDetailsId" placeholder="请输入计划详情id" />
        </el-form-item>
        <el-form-item label="标段id" prop="bdId">
          <el-input v-model="form.bdId" placeholder="请输入标段id" />
        </el-form-item>
        <el-form-item label="供应商id" prop="gysId">
          <el-input v-model="form.gysId" placeholder="请输入供应商id" />
        </el-form-item>
        <el-form-item label="材料编号" prop="manifestCode">
          <el-input v-model="form.manifestCode" placeholder="请输入材料编号" />
        </el-form-item>
        <el-form-item label="材料名称" prop="manifestName">
          <el-input v-model="form.manifestName" placeholder="请输入材料名称" />
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="本月计划采购数量" prop="monthPlanPurchaseNumber">
          <el-input v-model="form.monthPlanPurchaseNumber" placeholder="请输入本月计划采购数量" />
        </el-form-item>
        <el-form-item label="本次供应数量" prop="onceSupplyNumber">
          <el-input v-model="form.onceSupplyNumber" placeholder="请输入本次供应数量" />
        </el-form-item>
        <el-form-item label="本月累计供应数量" prop="monthTotalSupplyNumber">
          <el-input v-model="form.monthTotalSupplyNumber" placeholder="请输入本月累计供应数量" />
        </el-form-item>
        <el-form-item label="交接验收单据号" prop="acceptanceCode">
          <el-input v-model="form.acceptanceCode" placeholder="请输入交接验收单据号" />
        </el-form-item>
        <el-form-item label="供货日期" prop="supplyDate">
          <el-date-picker clearable v-model="form.supplyDate" type="date" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择供货日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listMaterialProgressDetails, getMaterialProgressDetails, delMaterialProgressDetails, addMaterialProgressDetails, updateMaterialProgressDetails } from "@/api/ma/materialProgressDetails";

export default {
  name: "MaterialProgressDetails",
  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 材料进度详情表格数据
      materialProgressDetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planDetailsId: undefined,
        bdId: undefined,
        gysId: undefined,
        type: undefined,
        manifestCode: undefined,
        manifestName: undefined,
        model: undefined,
        specification: undefined,
        unit: undefined,
        monthPlanPurchaseNumber: undefined,
        onceSupplyNumber: undefined,
        monthTotalSupplyNumber: undefined,
        acceptanceCode: undefined,
        supplyDate: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        progressDetailsId: [
          { required: true, message: "进度详情id不能为空", trigger: "blur" }
        ],
        planDetailsId: [
          { required: true, message: "计划详情id不能为空", trigger: "blur" }
        ],
        bdId: [
          { required: true, message: "标段id不能为空", trigger: "blur" }
        ],
        gysId: [
          { required: true, message: "供应商id不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型id不能为空", trigger: "change" }
        ],
        manifestCode: [
          { required: true, message: "材料编号不能为空", trigger: "blur" }
        ],
        manifestName: [
          { required: true, message: "材料名称不能为空", trigger: "blur" }
        ],
        model: [
          { required: true, message: "型号不能为空", trigger: "blur" }
        ],
        specification: [
          { required: true, message: "规格不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        monthPlanPurchaseNumber: [
          { required: true, message: "本月计划采购数量不能为空", trigger: "blur" }
        ],
        onceSupplyNumber: [
          { required: true, message: "本次供应数量不能为空", trigger: "blur" }
        ],
        monthTotalSupplyNumber: [
          { required: true, message: "本月累计供应数量不能为空", trigger: "blur" }
        ],
        acceptanceCode: [
          { required: true, message: "交接验收单据号不能为空", trigger: "blur" }
        ],
        supplyDate: [
          { required: true, message: "供货日期不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询材料进度详情列表 */
    getList () {
      this.loading = true;
      listMaterialProgressDetails(this.queryParams).then(response => {
        this.materialProgressDetailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        progressDetailsId: undefined,
        planDetailsId: undefined,
        bdId: undefined,
        gysId: undefined,
        type: undefined,
        manifestCode: undefined,
        manifestName: undefined,
        model: undefined,
        specification: undefined,
        unit: undefined,
        monthPlanPurchaseNumber: undefined,
        onceSupplyNumber: undefined,
        monthTotalSupplyNumber: undefined,
        acceptanceCode: undefined,
        supplyDate: undefined,
        createBy: undefined,
        updateBy: undefined,
        updateTime: undefined,
        createTime: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.progressDetailsId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加材料进度详情";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      const progressDetailsId = row.progressDetailsId || this.ids;
      getMaterialProgressDetails(progressDetailsId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改材料进度详情";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.progressDetailsId != null) {
            updateMaterialProgressDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addMaterialProgressDetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const progressDetailsIds = row.progressDetailsId || this.ids;
      this.$modal.confirm('是否确认删除材料进度详情编号为"' + progressDetailsIds + '"的数据项？').then(() => {
        this.loading = true;
        return delMaterialProgressDetails(progressDetailsIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('ma/materialProgressDetails/export', {
        ...this.queryParams
      }, `materialProgressDetails_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>
